/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sync.online;

import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.photos.PhotoEntry;
import com.google.gdata.util.ServiceException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import picasaphotomanager.PicasaSessionManager;
import picasaphotomanager.PicasawebClient;
import sync.exceptions.MergeException;

/**
 *
 * @author zalem1
 */
public class PicasaWebAlbumCache {
    private static HashMap<String, List<AlbumEntry>> _albumCache = new HashMap<String, List<AlbumEntry>>();
    private static HashMap<String, List<PhotoEntry>> _photoCache = new HashMap<String, List<PhotoEntry>>();
    
    public static void refreshAlbums() throws MergeException {
        for (String picasaAccountUsername : PicasaSessionManager.GetPicasaAccounts())
        {
            PicasawebClient client = PicasaSessionManager.GetClient(picasaAccountUsername);
            try {
                List<AlbumEntry> albumCollection = client.getAlbums();
                _albumCache.put(picasaAccountUsername, albumCollection);

            } catch (IOException ex) {
                throw new MergeException("Cannot connect to the remote repository.", ex);
            } catch (ServiceException ex) {
                throw new MergeException("Unexpected problem while connecting to the service.", ex);
            }
        }
    }

    public static AlbumEntry getAlbumEntry(String albumPath, String owner) {
        List<AlbumEntry> albumCollection = _albumCache.get(owner);
        String albumName = PicasaWebAlbumUtils.getFileName(albumPath);
        
        for (AlbumEntry album : albumCollection) {
            if (album.getTitle().getPlainText().equals(albumName)) {
                return album;
            }
        }
        
        return null;
    }
    
    
    public static PhotoEntry getMediaEntry(String mediaPath, String owner, String thumbnailResolution, String mediaResolution) throws MergeException {
        String mediaName = PicasaWebAlbumUtils.getFileName(mediaPath);
        
        String albumPath = PicasaWebAlbumUtils.getParentPath(mediaPath);
        AlbumEntry album = getAlbumEntry(albumPath, owner);
        if (album == null) return null;
        
        List<PhotoEntry> photoCollection;
        if (_photoCache.containsKey(album.getId())) {
            photoCollection = _photoCache.get(album.getId());
        }
        else {
            PicasawebClient client = PicasaSessionManager.GetClient(owner);
            try {
                photoCollection = client.getPhotos(album, thumbnailResolution, mediaResolution);
            } catch (IOException ex) {
                throw new MergeException("IO Exception", ex);
            } catch (ServiceException ex) {
                throw new MergeException("Service exception", ex);
            }
            _photoCache.put(album.getId(), photoCollection);
        }
            
        for (PhotoEntry photo : photoCollection) {
            if (photo.getTitle().getPlainText().equals(mediaName))
                return photo;
        }
        
        return null;
    }    
}
